//
// Copyright (c) 2004 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees.  This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents.  Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and BroadVision One-To-One are registered
// trademarks of BroadVision, Inc., in the United States and the European
// Community, and are trademarks of BroadVision, Inc., in other
// countries.  The BroadVision logo, is a trademark of BroadVision, Inc.,
// in the United States and other countries. Additionally, IONA and Orbix
// are trademarks of IONA Technologies, Ltd.  RSA, MD5, and RC2 are
// trademarks of RSA Data Security, Inc.
//

package com.broadvision.manage.tools;

import java.util.MissingResourceException;
import com.broadvision.basic.log.LoggerImpl;
import com.broadvision.basic.log.Logger;
import com.broadvision.basic.log.LogLevel;

/**
 * ToolsLog
 *
 * Provide interface to LoggerImpl.java for thin-client tools.
 *
 */ 
public class ToolsLog {

  /**
   *
   */
  public static final String NO_LOG_ERROR = "ERROR : CAN'T GET LOGGER";

  /**
   *
   */
  public static final String LOG_RESOURCE = "com.broadvision.manage.tools.Tools";

  /**
   *
   */
  public static final String LOG_LABEL = "Tools";

  /**
   * to output the debug message to log
   * 
   * @param css the class of caller
   * @param msg debug message
   */
  public static void debug(Class css, String msg) {
    Logger logger = LoggerImpl.getLogger(css);
    if (logger == null) {
      System.err.println(NO_LOG_ERROR);
      return;
    }
    logger.debug(msg);
  }

  /**
   * Check whether debugging is turned on for the calling class.
   */
  public static boolean isDebug(Class css) {
    Logger logger = LoggerImpl.getLogger(css);
    if (logger == null) {
      System.err.println(NO_LOG_ERROR);
      return false;
    }
    return logger.isLoggable(LogLevel.DEBUG);
  }

  /**
   * to output the debug message to log
   * 
   * @param css the class of caller
   * @param methodName the method name debug execute from
   * @param msg debug message
   */
  public static void debug(Class css, String methodName, String msg) {
    Logger logger = LoggerImpl.getLogger(css);
    String debugMessage = methodName + ": " + msg;
    if (logger == null) {
      System.err.println(NO_LOG_ERROR);
      return;
    }
    logger.debug(debugMessage);
  }
	
  /**
   * to output info message to log
   *
   * @param css the class of caller
   * @param methodName the method name info execute from
   * @param msgId debug message Id specified in the resource
   * @param msg extra message log into the log 
   */
  public static void info(Class css,
                          String methodName,
                          int msgId,
                          String msg) {
    try {
      Logger logger = LoggerImpl.getLogger(css, ToolsLog.LOG_RESOURCE);
      String cssName = css.getName(); 
      if (logger == null) {
        System.err.println(NO_LOG_ERROR);
        return;
      }

      if (msg == null) {
        logger.info(ToolsLog.LOG_LABEL, msgId, methodName, cssName);
      }
      else {
        logger.log(ToolsLog.LOG_LABEL,
                   LogLevel.INFO,
                   Tools_ErrorsDefine.AS_IS_MESSAGE,
                   methodName,
                   css.getName(),
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output info message to log
   *
   * @param css the class of caller
   * @param methodName the method name info execute from
   * @param msg extra message log into the log 
   */
  public static void info(Class css,
                          String methodName,
                          String msg) {
    try {
      Logger logger = LoggerImpl.getLogger(css, ToolsLog.LOG_RESOURCE);
      String cssName = css.getName(); 

      if (logger == null) {
        System.err.println(NO_LOG_ERROR);
        return;
      }

      if (msg == null) {
        logger.info(ToolsLog.LOG_LABEL, Tools_ErrorsDefine.AS_IS_MESSAGE,
                    methodName, cssName);
      }
      else {
        Error err = new Error(msg);
        logger.info(ToolsLog.LOG_LABEL, Tools_ErrorsDefine.AS_IS_MESSAGE,
                    methodName, cssName, err);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output error message to error log
   *
   * @param css the class of caller
   * @param methodName the method name error execute from
   * @param msgId debug message Id specified in the resource
   * @param msg extra message log into the error log 
   */
  public static void error(Class css,
                           String methodName,
                           int msgId,
                           String msg) {
    try {
      Logger logger = LoggerImpl.getLogger(css, ToolsLog.LOG_RESOURCE);
      String cssName = css.getName();

      if (logger == null) {
        System.err.println(NO_LOG_ERROR);
        return;
      }

      if (msg == null) {
        logger.error(ToolsLog.LOG_LABEL, msgId, methodName, cssName);
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.ERROR, 
                   msgId, 
                   methodName, 
                   cssName, 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output error message to error log
   *
   * @param css the class of caller
   * @param methodName the method name error execute from
   * @param msg extra message log into the error log 
   */
  public static void error(Class css,
                           String methodName,
                           String msg) {
    try {
      Logger logger = LoggerImpl.getLogger(css, ToolsLog.LOG_RESOURCE);
      String cssName = css.getName(); 

      if (logger == null) {
        System.err.println(NO_LOG_ERROR);
        return;
      }

      if (msg == null) {
        logger.error(ToolsLog.LOG_LABEL, Tools_ErrorsDefine.AS_IS_MESSAGE,
                     methodName, cssName);
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.ERROR, 
                   Tools_ErrorsDefine.AS_IS_MESSAGE, 
                   methodName, 
                   cssName, 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output error message to error log
   *
   * @param css the class of caller
   * @param methodName the method name error execute from
   * @param msgId debug message Id specified in the resource
   */
  public static void error(Class css,
                           String methodName,
                           int msgId) {
    try {
      Logger logger = LoggerImpl.getLogger(css, ToolsLog.LOG_RESOURCE);
      String cssName = css.getName();

      if (logger == null) {
        System.err.println(NO_LOG_ERROR);
        return;
      }
      logger.error(ToolsLog.LOG_LABEL, msgId, methodName, cssName);
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  private Logger logger; 
  private Class css;

  /**
   * Constructor that stores the classname so the log can be used non-statically.
   */
  public ToolsLog(Class css) {
    this.css = css;
    this.logger = LoggerImpl.getLogger(css, LOG_RESOURCE);
    if (logger == null) {
      System.err.println(NO_LOG_ERROR);
      throw new NullPointerException("Null logger");
    }
  }

  /**
   *
   */
  public Logger getLogger() { return logger; }

  /**
   * to output the debug message to log
   * 
   * @param msg debug message
   */
  public void debug(String msg) {
    logger.debug(msg);
  }

  /**
   * Check whether debugging is turned on for the calling class.
   */
  public boolean isDebug() {
    return logger.isLoggable(LogLevel.DEBUG);
  }

  /**
   * to output the debug message to log
   * 
   * @param methodName the method name error execute from
   * @param msg debug message
   */
  public void debug(String methodName, String msg) {
    String debugMessage = methodName + ": " + msg;
    logger.debug(debugMessage);
  }
	
  /**
   * to output info message to log
   *
   * @param methodName the method name error execute from
   * @param msgId debug message Id specified in the resource
   * @param msg extra message log into the log 
   */
  public void info(String methodName,
                   int msgId,
                   String msg) {
    try {
      if (msg == null) {
        logger.info(ToolsLog.LOG_LABEL, msgId, methodName, css.getName());
      }
      else {
        logger.log(ToolsLog.LOG_LABEL,
                   LogLevel.INFO,
                   Tools_ErrorsDefine.AS_IS_MESSAGE,
                   methodName,
                   css.getName(),
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output info message to log
   *
   * @param methodName the method name info execute from
   * @param msg extra message log into the log 
   */
  public void info(String methodName, String msg) {
    try {
      if (msg == null) {
        logger.info(ToolsLog.LOG_LABEL, Tools_ErrorsDefine.AS_IS_MESSAGE,
                    methodName, css.getName());
      }
      else {
        logger.log(ToolsLog.LOG_LABEL,
                   LogLevel.INFO,
                   Tools_ErrorsDefine.AS_IS_MESSAGE,
                   methodName,
                   css.getName(),
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output error message to error log
   *
   * @param methodName the method name error execute from
   * @param msgId debug message Id specified in the resource
   * @param msg extra message log into the error log 
   */
  public void error(String methodName,
                    int msgId,
                    String msg) {
    try {
      if (msg == null) {
        logger.error(ToolsLog.LOG_LABEL, msgId, methodName, css.getName());
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.ERROR, 
                   msgId, 
                   methodName, 
                   css.getName(), 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output error message to error log
   *
   * @param methodName the method name error execute from
   * @param msg extra message log into the error log 
   */
  public void error(String methodName,
                    String msg) {
    try {
      if (msg == null) {
        logger.error(ToolsLog.LOG_LABEL, Tools_ErrorsDefine.AS_IS_MESSAGE,
                     methodName, css.getName());
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.ERROR, 
                   Tools_ErrorsDefine.AS_IS_MESSAGE, 
                   methodName, 
                   css.getName(), 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output error message to error log
   *
   * @param methodName the method name error execute from
   * @param msgId debug message Id specified in the resource
   */
  public void error(String methodName,
                    int msgId) {
    try {
      logger.error(ToolsLog.LOG_LABEL, msgId, methodName, css.getName());
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  // warn messages

  /**
   * to output warn message to warn log
   *
   * @param css the class of caller
   * @param methodName the method name warn execute from
   * @param msgId debug message Id specified in the resource
   * @param msg extra message log into the warn log 
   */
  public static void warn(Class css,
                          String methodName,
                          int msgId,
                          String msg) {
    try {
      Logger logger = LoggerImpl.getLogger(css, ToolsLog.LOG_RESOURCE);
      String cssName = css.getName(); 
      if (logger == null) {
        System.err.println(NO_LOG_ERROR);
        return;
      }

      if (msg == null) {
        logger.warn(ToolsLog.LOG_LABEL, msgId, methodName, cssName);
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.WARN, 
                   msgId, 
                   methodName, 
                   cssName, 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output warn message to warn log
   *
   * @param methodName the method name warn execute from
   * @param msgId debug message Id specified in the resource
   * @param msg extra message log into the warn log 
   */
  public void warn(String methodName,
                   int msgId,
                   String msg) {
    try {
      if (msg == null) {
        logger.warn(ToolsLog.LOG_LABEL, msgId, methodName, css.getName());
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.WARN, 
                   msgId, 
                   methodName, 
                   css.getName(), 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output warn message to warn log
   *
   * @param methodName the method name warn execute from
   * @param msg extra message log into the warn log 
   */
  public void warn(String methodName, String msg) {
    try {
      if (msg == null) {
        logger.warn(ToolsLog.LOG_LABEL, Tools_ErrorsDefine.AS_IS_MESSAGE,
                    methodName, css.getName());
      }
      else {
        logger.log(ToolsLog.LOG_LABEL, 
                   LogLevel.WARN, 
                   Tools_ErrorsDefine.AS_IS_MESSAGE, 
                   methodName, 
                   css.getName(), 
                   msg);
      }
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * to output warn message to warn log
   *
   * @param methodName the method name warn execute from
   * @param msgId debug message Id specified in the resource
   */
  public void warn(String methodName, int msgId) {
    try {
      logger.warn(ToolsLog.LOG_LABEL, msgId, methodName, css.getName());
    }
    catch (MissingResourceException e) {
      e.getClassName();
    }
  }

  /**
   * Log a Throwable for this class, log level, method etc.
   *
   * @param level
   * @param methodName
   * @param t
   */
  public void log(LogLevel level,
                  String methodName,
                  Throwable t) {
    logger.log(ToolsLog.LOG_LABEL,
               level,
               Tools_ErrorsDefine.AS_IS_MESSAGE,
               methodName,
               css.getName(), t);
  }
}

